#
#   Character.py
#
#   This program is free software; you can redistribute it and/or modify
#   it under the terms of the GNU General Public License as published by
#   the Free Software Foundation; version 2 of the License.
#
#   This program is distributed in the hope that it will be useful,
#   but WITHOUT ANY WARRANTY; without even the implied warranty of
#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#   GNU General Public License for more details.
#
#   Copyright
#   Author: Nicholas F. Hoover
#   Contributors: Salvatore S. Gionfriddo
#   Created: 2007.06.30
#   Last Modified: 2007.07.19
#

import pygame
from GameObject import GameObject


class Character(GameObject):
    """This is a GameObject with heatlh"""
    
    MIN_HEALTH = 0
    MAX_HEALTH = 100

    def __init__(self, fps = 30):
        GameObject.__init__(self, fps)
        self.reset()
        
    def incHealth(self,amount):
        self.health += amount
        if self.health > self.MAX_HEALTH: self.health = self.MAX_HEALTH
        
    def decHealth(self,amount):
        self.health -= amount
        if self.health < self.MIN_HEALTH: self.health = self.MIN_HEALTH        

    def isAlive(self):
        return self.health > self.MIN_HEALTH


    def reset(self):
        GameObject.reset(self)
        self.health = self.MAX_HEALTH

    def move(self, amount, collision = None, avert = False):
        (x,y) = amount

        choices = [(x,y), (0,y), (x,0), (y,x)]

        if collision:
            collision = collision[:]
            collision.remove(self.collision_rect)
            
        for choice in choices:
            (nX,nY) = choice
            can_move = True
            new_col_rect = pygame.Rect(self.collision_rect)
            new_col_rect.left += nX
            new_col_rect.top += nY
        
            #check collisions
            if collision:
                collide = new_col_rect.collidelist(collision)
                if collide >= 0:
                    can_move = False
            #move
            if can_move:
                self.setPosition((self.rect.left + nX, self.rect.top + nY))
                if self.collision_rect.collidelist(collision) >= 0: print 'WTF'
                break
            
            if not avert: break
